package com.example.controller;

import com.example.entity.ComponentTemplate;
import com.example.entity.Result;
import com.example.service.ComponentTemplateService;
//import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(value = "组件模板管理", description = "组件模板的增删改查")
@RestController
@RequestMapping(value = "/api/component-templates", produces = MediaType.APPLICATION_JSON_VALUE)
public class ComponentTemplateController {

    @Autowired
    private ComponentTemplateService componentTemplateService;

    @PostMapping
    public Result<String> create(@RequestBody ComponentTemplate componentTemplate) {
        int result = componentTemplateService.create(componentTemplate);
        return result > 0 ? Result.success() :Result.fail("创建失败");
    }

    @PutMapping("/{id}")
    public Result<String> update(@PathVariable Long id, @RequestBody ComponentTemplate componentTemplate) {
        componentTemplate.setId(id);
        int result = componentTemplateService.update(componentTemplate);
        return result > 0 ? Result.success() : Result.fail("更新失败");
    }

    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        int result = componentTemplateService.delete(id);
        return result > 0 ? Result.success() : Result.fail("删除失败");
    }

    @GetMapping("/{id}")
    public Result<ComponentTemplate> getById(@PathVariable Long id) {
        ComponentTemplate componentTemplate = componentTemplateService.getById(id);
        return componentTemplate != null ? Result.success(componentTemplate) : Result.success(null);
    }

    @GetMapping("/by-page-template/{pageTemplateId}")
    public Result<List> getByPageTemplateId(@PathVariable Long pageTemplateId) {
        List<ComponentTemplate> list = componentTemplateService.getByPageTemplateId(pageTemplateId);
        return Result.success(list);
    }

    @GetMapping
    public Result<List> getAll() {
        List<ComponentTemplate> list = componentTemplateService.getAll();
        return Result.success(list);
    }
}

